package com.authine.cloudpivot.web.Utils;

import org.apache.commons.codec.binary.Hex;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @description:
 * @author: zyj
 * @time: 2021/12/7 11:21
 */
public class ATSSignatureUtil {

    public static String getATSSignature(LocalDateTime now,String user_key,String secretKey,String data){
        String call_time = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS").format(now);
        String signatureStr = "user_key"+user_key+"data"+data+"call_time"+call_time+secretKey;
        String result = signatureStr.replaceAll(" ", "");
        String signature = stringToMD5(result);
        return signature;
    }

    private static String stringToMD5(String plainText) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            return Hex.encodeHexString(md.digest(plainText.getBytes(StandardCharsets.UTF_8)));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}
